
(function($) {
	/**
	 * @author Maowl
	 * jQuery styled checkbox
	 */
    $.fn.labelCheckbox = function(options_events){
		var opts = $.extend({}, 
							{}, options_events);
		
		$(this).children('input[type="checkbox"]').hide();
		
		$(this).each(function(k, v){
			var checkbox = $(v).children('input[type="checkbox"]}');
			if(checkbox.attr('checked') == 'checked'){
				$(this).removeClass('c_off').addClass('c_on');
			}else{
				$(this).removeClass('c_on').addClass('c_off');
			}
			$(v).click(function(event){
				event.preventDefault();
				
				checkbox.attr({
					'checked': (checkbox.attr('checked') == 'checked')? false : 'checked'
				});
				
				if(checkbox.attr('checked') == 'checked'){
					$(this).removeClass('c_off').addClass('c_on');
				}else{
					$(this).removeClass('c_on').addClass('c_off');
				}
			});			
		});
		
		$(this).children('input[type="checkbox"]').click(function(event){
			event.preventDefault();
			return false;
		});
	};
	
	/**
	 * @author Maowl
	 * @param {Object} options_events
	 */
	$.fn.picPortionResize = function(options_events){
		var opts = $.extend({}, 
							{	
								/*options*/
								staticSide: 'width',	//can be 'width' or 'height'
								setting: {
									staticVal: 100, 
									anotherMax: 200  //can be a number, or 'parent'
								},
								keepSmaller: false
							},
							options_events);
		
		return $(this).each(function(){
			var newImg = new Image();
			newImg.src = $(this)[0].src

			if(opts.staticSide == 'width'){
				var maxHeight = (opts.setting.anotherMax == 'parent')? $(this).parent().height() : opts.setting.anotherMax;
				if(!(newImg.width < opts.setting.staticVal && opts.keepSmaller)){
					var resizeWidth = opts.setting.staticVal;		
					var resizeHeight = newImg.height / newImg.width * resizeWidth;
					if(resizeHeight > maxHeight){
						resizeHeight = maxHeight;
					}
					
					$(this).css('width', resizeWidth);
					$(this).css('height', resizeHeight);
				}
			}else{
				var maxWidth = (opts.setting.anotherMax == 'parent')? $(this).parent().width() : opts.setting.anotherMax;
				if(!(newImg.height < opts.setting.staticVal && opts.keepSmaller)){
					var resizeHeight = opts.setting.staticVal;		
					var resizeWidth = newImg.width / newImg.height * resizeHeight;
					if(resizeWidth > maxWidth){
						resizeWidth = maxWidth;
					}
					
					$(this).css('width', resizeWidth);
					$(this).css('height', resizeHeight);
				}
			}
		});
	};
	
	/**
	 * @author Maowl
	 * 
	 * 根据回传的json_endo形式的$_POST, 将值写回表单
	 * 
	 * usage:
	 	var FormInitModel = new FormInit(<?php echo json_encode($_POST); ?>);
		FormInitModel.transverArrtoForm();
		
		如果$_POST中有数组形式的值，表单元素需要一个和name相同的class，如: <input name="group[]" class="group" />
	 */
	window.FormInit = function(_formData){
		this.formData = _formData;
		
		this.TransverArrFunc = function (Arr, Arrvalname){
			var existCount = jQuery('.'+Arrvalname).size();
			var thisObj = this;
			
			jQuery.each(Arr, function(name, val){
				if(typeof(val) == 'object'){
					thisObj.TransverArrFunc(val, name);
				}else{
					if(Arrvalname != null){
						if(thisObj.formData['clickId'][Arrvalname] != null){
							if(name >= existCount-1){
								jQuery('#'+thisObj.formData['clickId'][Arrvalname]).click();
							}
						}
					}
					
					if(Arrvalname == null){
						jQuery('[name="'+name+'"]').val(val);
					}else{
						jQuery('.'+Arrvalname).eq(name).val(val);
					}
					
				}
			});
		};
		
		this.transverArrtoForm = function(){
			this.TransverArrFunc(this.formData, null);
		}
	};
	
	/**
	 * @author Alexander Farkas
	 * jQuery background-position animate
	 * v. 1.02
	 */
	$.extend($.fx.step,{
		backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
                        }
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
	

})(jQuery);












$(document).ready(function(){
	//menu animation
	$('.rightMenu .hoveranimate').css({
		background: "url('/images/bg_menu_li.gif') no-repeat",
		'background-position': '0 7px',
		'min-height': 0
	}).mouseover(function(){
		$(this).stop();
		$(this).animate({
			'backgroundPosition': '-14px 7px'
		}, 200, function(){
			$(this).animate({
				'backgroundPosition': '-14px -15px'
			}, 1, function(){
				$(this).animate({
					'backgroundPosition': '0 -15px'
				}, 200);
			});
		});
	}).mouseout(function(){
		$(this).stop();
		$(this).animate({
			'backgroundPosition': '-14px -15px'
		}, 200, function(){
			$(this).animate({
				'backgroundPosition': '-14px 7px'
			}, 1, function(){
				$(this).animate({
					'backgroundPosition': '0 7px'
				}, 200);
			});
		});
	});	
	
	
	$('.h180MaxWparent').picPortionResize({
		/*options*/
		staticSide: 'height',	//can be 'width' or 'height'
		setting: {
			staticVal: 180, 
			anotherMax: 'parent'  //can be a number, or 'parent'
		},
		keepSmaller: false		
	});
	
	//label_checkbox
	$('.label_check').labelCheckbox();
	
	//click when hover
	$('.hoverc').mouseover(function(){
		$(this).click();
	});
		
	function waterFall(obj, itemSelector)
	{
		if(obj.hasClass('masonry')){
			obj.masonry('reload');
		}else{
			obj.masonry({
				itemSelector: itemSelector,
				singleMode: true,
				resizeable: true,
				animate: true,
				isAnimated: true,
				animationOptions: {
					duration: 390,
					easing: 'linear',
					queue: false
				},
				isFitWidth: true
			}).addClass('masonry');
		}
	}
	if($('.waterFall').size() > 0){
		//waterFall($('.waterFall'), '.waterFallItem');
	}
	
	$('.hasTooltip').tooltip();
});
